﻿using System;
using SS.Common.DBBuilder.DBDataStructure;
using SS.Net.AppGen.Models;
using SS.Net.AppGen.Generators;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace SS.Net.AppGen.WebGenerators.WebViewGenerator
{
    public abstract class BaseDetailViewItemAlgorithm : BaseGenerator, IDetailViewItemAlgorithm
    {
        #region Fields

        public static class AbstractBaseConsts
        {
            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstScriptValidation = "[ScriptValidation]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstTableFields = "[TableFields]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstAttributeContent = "[AttributeContent]";

            /// <summary>
            /// This value will be replaced at run time
            /// </summary>
            public const string ConstForeignInputHidden = "[ForeignInputHidden]";
        }

        /// <summary>
        /// The instance of ViewComponents, this object will be updated when deserialization pattern file
        /// </summary>
        public ViewComponents ViewComponents { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Deserialize Base Controller pattern file
        /// </summary>
        public void DeserializeTemplate()
        {
            var patternFile = Path.Combine(this.GetAppPath(), "Templates", "WebTemplates", this.XMLTemplate);

            using (XmlTextReader xreader = new XmlTextReader(patternFile))
            {
                xreader.Namespaces = false;
                try
                {
                    var x = new XmlSerializer(typeof(ViewComponents));
                    this.ViewComponents = x.Deserialize(xreader) as ViewComponents;
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Cannot deserialize BaseDetailViewItemAlgorithm, BaseDetailViewItemAlgorithm => {0}", ex.Message));
                }
            }
        }

        /// <summary>
        /// Method for generating contents of partial list view components and save generated contents to a file
        /// </summary>
        /// <param name="tbl">The data source is corresponding with class which will be generated</param>
        public abstract void GenerateContents(Table tbl);

        #endregion
    }
}
